<?php
include 'user_update_lib.php';

$a_permission = get_passport_action_from_module($db_conn, array('CAP_NHAT_THONG_TIN'));
if ( !in_array('update-permission', $a_permission)) { raise_error('Bạn chưa được cấp quyền thực hiện chức năng này'); }

if ($s_Action == 'get-user') {

    // <editor-fold defaultstate="collapsed" desc="Get-User">
    $v_response_json       = new stdClass();
    $v_response_json->code = 0;
    $v_response_json->data = array ( );
    
    $s_Where  = "course = '" . $a_KhoaHoc_Info['start_year'] . "'";
    $s_Where .= $s_category_team  ? " and team  = '$s_category_team'    " : '';
    $s_Where .= $s_category_level ? " and level = '$s_category_level'   " : '';
    $s_Where .= $s_category_crew  ? " and crew  = '$s_category_crew'    " : '';
    
    $a_Option = array(
        'class_condition' => $s_Where,
        'user_condition'  => $f_usertype == 'THIEU_NHI' ? $s_get_THIEU_NHI_condition : $s_get_HUYNH_TRUONG_condition,
    );
    
    $a_User = array ( );
    $a_Tmp_User = get_class_to_user($a_Option, $db_conn);

    foreach ($a_Tmp_User as $s_UserID => $a_UserInfo) {
        $a_User[$a_UserInfo->team][$a_UserInfo->level][$a_UserInfo->crew][$a_UserInfo->userid] = $a_UserInfo;
    }

    $v_response_json->code = 200;
    $v_response_json->data = array (
        'users'    => $a_User,
    );
    echo json_encode($v_response_json, true);
    exit;
    // </editor-fold>
}
else if ($s_Action == 'update-eucharist-date') {

    // <editor-fold defaultstate="collapsed" desc="Update-Eucharist">
    $v_response_json       = new stdClass();
    $v_response_json->code = 0;
    $v_response_json->data = array ( );
    
    if (!is_array($f_userid)) { echo json_encode($v_response_json, true); exit; }
    
    $a_Row = array (
        'eucharist_date' => $f_eucharist_date,
        'updaterid'      => $s_UserID,
        'updatedate'     => $f_confirmation_date
    );

    db_update_row($db_conn, $a_Row, 'user',  "userid in ('".implode("', '", $f_userid)."')");

    $v_response_json->code = 200;
    $v_response_json->data = TRUE;
    
    echo json_encode($v_response_json, true);
    exit;
    // </editor-fold>
}
else if ($s_Action == 'update-confirmation-date') {

    // <editor-fold defaultstate="collapsed" desc="Update-Confirmation">
    $v_response_json       = new stdClass();
    $v_response_json->code = 0;
    $v_response_json->data = array ( );
    
    if (!is_array($f_userid)) { echo json_encode($v_response_json, true); exit; }

    $a_Row = array (
        'confirmation_date' => $f_confirmation_date,
        'updaterid'         => $s_UserID,
        'updatedate'        => $f_confirmation_date
    );

    db_update_row($db_conn, $a_Row, 'user',  "userid in ('".implode("', '", $f_userid)."')");

    $v_response_json->code = 200;
    $v_response_json->data = TRUE;
    
    echo json_encode($v_response_json, true);
    exit;
    // </editor-fold>
}
else if ($s_Action == 'update-new-class') {

    // <editor-fold defaultstate="collapsed" desc="Update-New-Class">
    $v_response_json       = new stdClass();
    $v_response_json->code = 0;
    $v_response_json->data = array ( );
    
    if (!is_array($f_userid)) { echo json_encode($v_response_json, true); exit; }
    
    $a_Row = array();

    foreach ($f_userid as $v_userid) {
        $a_Row[] = array (
            'userid'     => $v_userid,
            'course'     => $a_KhoaHoc_Info['start_year'] + 1,
            'team'       => $f_team,
            'level'      => $f_level,
            'crew'       => $f_crew,
            'creatorid'  => $s_UserID,
            'updaterid'  => $s_UserID,
            'insertdate' => $f_insertdate,
            'updatedate' => $f_insertdate,
        );
    }

    $a_Row_Duplicate = array(
        'course'     => $a_KhoaHoc_Info['start_year'] + 1,
        'team'       => $f_team,
        'level'      => $f_level,
        'crew'       => $f_crew,
        'updaterid'  => $s_UserID,
        'updatedate' => $f_insertdate,
    );

    db_insert_duplicate_row($db_conn, $a_Row, $a_Row_Duplicate, 'user_class');

    $v_response_json->code = 200;
    $v_response_json->data = TRUE;
    
    echo json_encode($v_response_json, true);
    exit;
    // </editor-fold>
}
else {
    raise_error('Bạn chưa được cấp quyền thực hiện chức năng này');
}
?>